package com.sgb.kjwl.view.ui.activity.vehicleManagement;

import android.app.AlertDialog;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;

import androidx.annotation.Nullable;
import androidx.core.util.Pair;
import androidx.databinding.DataBindingUtil;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.bm.library.PhotoView;
import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.sgb.kjwl.AppActivity;
import com.sgb.kjwl.R;
import com.sgb.kjwl.databinding.ActivityDriverInfoBinding;
import com.sgb.kjwl.di.vehicleManagement.AddVehicleModule;
import com.sgb.kjwl.di.vehicleManagement.DaggerAddVehicleComponent;
import com.sgb.kjwl.model.entity.DriverInfo;
import com.sgb.kjwl.utils.AbScreenUtils;
import com.sgb.kjwl.utils.ActivityJump;
import com.swgk.core.event.EventConstant;
import com.sgb.kjwl.view.event.self.AuthEvent;
import com.sgb.kjwl.view.event.self.AuthenticationEvent;
import com.sgb.kjwl.view.ui.activity.self.authentication.CertificationActivity;
import com.sgb.kjwl.view.widget.transport.ImagePreviewDialog;
import com.sgb.kjwl.viewmodel.vehicleManagement.DriverInfoAtModel;
import com.swgk.core.base.aroute.AppRouteConstant;
import com.swgk.core.base.di.AppComponent;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import javax.inject.Inject;

/**
 * 司机详情页
 */
@Route(path = AppRouteConstant.DriverInfoActivity)
public class DriverInfoActivity extends AppActivity {
    @Inject
    DriverInfoAtModel model;
    ActivityDriverInfoBinding binding;

    @Override
    protected void setupActivityComponent(AppComponent appComponent) {
        DaggerAddVehicleComponent.builder().appComponent(appComponent).addVehicleModule(new AddVehicleModule(this)).build().inject(this);
    }

    public static void start(Context context) {
        Intent intent = new Intent(context, DriverInfoActivity.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
        context.startActivity(intent);
    }

    @Override
    protected void onCreate(@Nullable @org.jetbrains.annotations.Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EventBus.getDefault().register(this);
    }

    @Override
    protected void initView() {
        binding = DataBindingUtil.setContentView(this, R.layout.activity_driver_info);
        binding.include.titleView.setText("司机认证详情");
        binding.include.rightView.setText("重新认证");
        binding.include.ivBack.setOnClickListener(this);
        binding.driverGoAuth.setOnClickListener(this);
        model.getInfo();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.iv_back:
                finish();
                break;
            case R.id.driver_go_auth:
                CertificationActivity.start(getActivity(), "1");
                break;
        }
    }

    public void setDataInfo(DriverInfo info) {
        binding.setViewModel(info);
        binding.nestedScrollView.setVisibility(View.VISIBLE);
        switch (info.getStatus()) {
            case 0:
                binding.ivStatus.setImageResource(R.drawable.cheng1);
                binding.tvStatus.setText("司机认证审核中");
                binding.include.rightView.setVisibility(View.GONE);
                binding.llReason.setVisibility(View.GONE);
                break;
            case 1:
                binding.tvStatus.setText("司机认证信息已通过");
                binding.ivStatus.setImageResource(R.drawable.cheng);
                binding.include.rightView.setVisibility(View.VISIBLE);
                binding.llReason.setVisibility(View.GONE);
                binding.include.rightView.setOnClickListener(view ->
                {
                    ActivityJump.jumpToActivity(this, AddDriverInfoActivity.class,
                            Pair.create("id", info.getId()),
                            Pair.create("name", info.getName()),
                            Pair.create("cardId", info.getIdCard()),
                            Pair.create("licenseMain", info.getMainDriverLicense()),
                            Pair.create("licenseDeputy", info.getViceDriverLicense()),
                            Pair.create("qualifyMain", info.getMainCertificate()),
                            Pair.create("qualifyDeputy", info.getViceCertificate()),
                            Pair.create("acquireTime", info.getAcquireTime()));
                });
                binding.llReason.setVisibility(View.GONE);
                break;
            case 2:
                binding.tvStatus.setText("司机认证审核不通过");
                binding.ivStatus.setImageResource(R.drawable.cheng2);
                binding.llReason.setVisibility(View.VISIBLE);
                LinearLayout.LayoutParams linearLayout = (LinearLayout.LayoutParams) binding.tvStatus.getLayoutParams();
                linearLayout.bottomMargin = AbScreenUtils.dp2px(this, 13);
                binding.include.rightView.setVisibility(View.VISIBLE);
                binding.include.rightView.setOnClickListener(view ->
                {
                    ActivityJump.jumpToActivity(this, AddDriverInfoActivity.class,
                            Pair.create("id", info.getId()),
                            Pair.create("name", info.getName()),
                            Pair.create("cardId", info.getIdCard()),
                            Pair.create("licenseMain", info.getMainDriverLicense()),
                            Pair.create("licenseDeputy", info.getViceDriverLicense()),
                            Pair.create("qualifyMain", info.getMainCertificate()),
                            Pair.create("qualifyDeputy", info.getViceCertificate()),
                            Pair.create("acquireTime", info.getAcquireTime()));
                });

                break;
        }

        if (info.getMainDriverLicense() != null && !info.getMainDriverLicense().isEmpty()) {
            binding.ivMain.setOnClickListener(view ->
                    ImagePreviewDialog.newInstance(info.getMainDriverLicense()).show(this)
            );
        }
        if (info.getViceDriverLicense() != null && !info.getViceDriverLicense().isEmpty()) {
            binding.ivDeputy.setOnClickListener(view -> ImagePreviewDialog.newInstance(info.getViceDriverLicense()).show(this));
        }
        if (info.getMainCertificate() != null && !info.getMainCertificate().isEmpty()) {
            binding.ivQualityMain.setOnClickListener(view -> ImagePreviewDialog.newInstance(info.getMainCertificate()).show(this));
        } else {
            binding.rlQualify.setVisibility(View.GONE);
        }
        if (info.getViceCertificate() != null && !info.getViceCertificate().isEmpty()) {
            binding.ivQualityDeputy.setOnClickListener(view -> ImagePreviewDialog.newInstance(info.getViceCertificate()).show(this));
        } else {
            binding.rlQualify.setVisibility(View.GONE);
        }
        if (TextUtils.isEmpty(info.getIdCard())){
            binding.llNameDriver.setVisibility(View.GONE);
            binding.llDriverIdentity.setVisibility(View.GONE);
            binding.llDriverAge.setVisibility(View.GONE);
            binding.llAge.setVisibility(View.GONE);
            binding.showAuthGo.setVisibility(View.VISIBLE);
        }else{
            binding.showAuthGo.setVisibility(View.GONE);
            binding.llNameDriver.setVisibility(View.VISIBLE);
            binding.llDriverIdentity.setVisibility(View.VISIBLE);
            if (!TextUtils.isEmpty(info.getRealAge())) {
                binding.llAge.setVisibility(View.VISIBLE);
                binding.tvAge.setText(info.getRealAge());
            }
            if (!TextUtils.isEmpty(info.getDriverAge())) {
                binding.llDriverAge.setVisibility(View.VISIBLE);
                binding.tvDriverAge.setText(info.getDriverAge());
            }
        }
        binding.tvName.setText(info.getName());
        binding.tvCardId.setText(info.getIdCard());
        binding.tvFileNumber.setText(info.getFileNumber());
        binding.tvAddress.setText(info.getDetailAddress());
    }


    private void showDialog(String url) {
        LayoutInflater inflater = LayoutInflater.from(this);
        View layout = inflater.inflate(R.layout.dialog_show_picture, null);
        AlertDialog dialog;
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setView(layout);
        builder.setCancelable(true);
        dialog = builder.show();
        dialog.getWindow().setBackgroundDrawableResource(android.R.color.transparent);

//        DisplayMetrics dm = new DisplayMetrics();
//        //获取屏幕信息
//        dm = this.getResources().getDisplayMetrics();
//        int screenWidth = dm.widthPixels;
//        int screenHeigh = dm.heightPixels;

        PhotoView imageView = dialog.findViewById(R.id.iv_show_large);
        imageView.enable();
        LinearLayout.LayoutParams linearParams = (LinearLayout.LayoutParams) imageView.getLayoutParams(); //取控件textView当前的布局参数
//        linearParams.width = screenWidth;//
//        linearParams.height = screenHeigh;
        imageView.setLayoutParams(linearParams);
        imageView.setOnClickListener(view -> dialog.dismiss());
        RequestOptions requestOptions = new RequestOptions();
        requestOptions.placeholder(R.drawable.null_img_list).fallback(R.drawable.null_img_list).error(R.drawable.null_img_list);
        Glide.with(imageView.getContext()).load(url).apply(requestOptions).into(imageView);

    }

    @Subscribe(threadMode = ThreadMode.MAIN, sticky = true)
    public void onEvent(AuthEvent event) {
        if (event.getMessageTag() == EventConstant.EVENT_REFRESH_DRIVER_AUTH_) {
            model.getInfo();
        }
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }
    /**
     * event监听用户的认证的状态
     */

    @Subscribe(threadMode = ThreadMode.MAIN, sticky = true)
    public void onEvent(AuthenticationEvent event) {
        if (event.getMessageTag() == EventConstant.EVENT_USER_AUTHENTICATION) {
            model.getInfo();
        }
    }

}
